package com.zcxx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @Author WangShuai
 * @Date 2025/3/4 17:35
 * @Version 1.0
 * @Description 配置Spring Security的类
 * 用于定义安全相关的Bean，比如密码编码器
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().permitAll() // 所有请求都允许访问
                .and()
                .csrf().disable(); // 禁用CSRF保护
    }

    /**
     * 创建并返回一个PasswordEncoder实例
     * 该实例用于编码和匹配密码，这里使用BCrypt算法来实现
     * BCrypt是一种强大的哈希算法，适用于安全地存储密码
     *
     * @return PasswordEncoder实例，用于密码处理
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

//    // 配置用户信息（内存存储）
//    @Bean
//    @Override
//    public UserDetailsService userDetailsService() {
//        UserDetails user = User.builder()
//                .username("user")
//                .password(passwordEncoder().encode("123456")) // 加密密码
//                .roles("USER")  // 角色为 USER
//                .build();
//
//        UserDetails admin = User.builder()
//                .username("admin")
//                .password(passwordEncoder().encode("admin123"))
//                .roles("ADMIN", "USER")  // 可拥有多个角色
//                .build();
//
//        return new InMemoryUserDetailsManager(user, admin);
//    }

    // 配置 HTTP 请求权限
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http
//                .authorizeRequests()
//                .antMatchers("/", "/login", "/static/**", "/css/**").permitAll()
//                .antMatchers("/admin/**").hasRole("ADMIN")  // 只允许 ADMIN 角色访问 /admin/**
//                .antMatchers("/user/**").hasAnyRole("USER", "ADMIN")  // 允许 USER 和 ADMIN 角色访问 /user/**
//                .anyRequest().authenticated()
//                .and()
//                .formLogin()
//                .loginPage("/login")
//                .defaultSuccessUrl("/", true)
//                .and()
//                .logout()
//                .logoutSuccessUrl("/")
//                .and()
//                .csrf().disable()
//                .sessionManagement()
//                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED); // 确保会话创建策略正确
//    }
}
